﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TCHSHue.DAL;

namespace TCHSHue.BLL
{
    public class LKWSBLL
    {
        public static List<LKWebsite> getList()
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = from e in data.LKWebsites
                         orderby e.tenHienThi ascending
                         select e;
                return ls.ToList();
            }
        }
        public static bool create(string tenHienThi, string url)
        {
            if (!string.IsNullOrEmpty(tenHienThi) && !string.IsNullOrEmpty(url))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var ls = (from e in data.LKWebsites
                              where e.url == url
                              select e).SingleOrDefault();
                    if (ls != null)
                        return false;
                    LKWebsite lk= new LKWebsite{ tenHienThi= tenHienThi,url=url};
                    data.LKWebsites.InsertOnSubmit(lk);
                    data.SubmitChanges();
                    return true;
                }
            }
            return false;
        }

        internal static bool delete(string maLK)
        {
            int temp = 0;
            if (int.TryParse(maLK, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var lk = (from e in data.LKWebsites
                                  where e.maLK== int.Parse(maLK)
                                  select e).SingleOrDefault();
                    if (lk != null)
                    {
                        data.LKWebsites.DeleteOnSubmit(lk);
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            else
                return false;
        }

        internal static bool update(string maLK, string tenHienThi, string url)
        {
            int temp = 0;
            if (int.TryParse(maLK, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var lk = (from e in data.LKWebsites
                                  where e.maLK== int.Parse(maLK)
                                  select e).SingleOrDefault();
                    if (lk != null)
                    {
                        lk.tenHienThi= tenHienThi;
                        lk.url = url;
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            else
                return false;
        }

        internal static LKWebsite getLK(string maLK)
        {
            int temp = 0;
            if (int.TryParse(maLK, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var lk = (from e in data.LKWebsites
                                  where e.maLK == int.Parse(maLK)
                                  select e).SingleOrDefault();
                    return lk;
                }
            }
            return null;
        }
    }
}